<?php
/**
* @package plugin Frontend-User-Access (plugin for component Frontend-User-Access)
* @version 3.0.0
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro 
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentFrontenduseraccess extends JPlugin{

	var $database;
	var $fua_config;
	var $user_id;
	var $user_type;

	function plgContentFrontenduseraccess(& $subject, $config){
		parent::__construct($subject, $config);
		$this->database = JFactory::getDBO();
		
		//get config		
		$this->fua_config = $this->get_config();
		
		//get user id
		$user =& JFactory::getUser();		
		$this->user_id = $user->get('id');
		$this->user_type = $user->get('usertype');
	}
	
	function get_config(){			
		
		$this->database->setQuery("SELECT config "
		."FROM #__fua_config "
		."WHERE id='fua' "
		."LIMIT 1"
		);		
		$raw = $this->database->loadResult();		
		
		$params = explode( "\n", $raw);
		
		for($n = 0; $n < count($params); $n++){		
			$temp = explode('=',$params[$n]);
			$var = $temp[0];
			$value = '';
			if(count($temp)==2){
				$value = trim($temp[1]);
				if($value=='false'){
					$value = false;
				}
				if($value=='true'){
					$value = true;
				}
			}							
			$config[$var] = $value;	
		}		
				
		return $config;			
	}

	function onAfterDisplayContent(){
		return 'frontend_user_access_hide_article_end';
	}
	
	function onPrepareContent( &$row, &$params, $page=0 ){
	
		$option = JRequest::getVar('option', '');	
		$view = JRequest::getVar('view', '');	
		$layout = JRequest::getVar('layout', '');
		if(!($option=='com_content' && $view=='article' && ($layout=='default' || $layout==''))){	
			echo 'frontend_user_access_article_hide_start';	
		}
		
		$row_id = 0;
		if (is_object( $row )) {
			 if (isset( $row->id ))
			 {
				$row_id = $row->id;
			}
		}
		
		if($this->fua_config['items_active'] || $this->fua_config['categories_active'] || $this->fua_config['sections_active']){						
			
			$option = JRequest::getVar('option', '');	
			$view = JRequest::getVar('view', '');	
			$layout = JRequest::getVar('layout', '');				
		
			if($this->user_type!='Super Administrator' && 
			//only check com_content
			($option=='com_content' && 
			//category blog
			(($view=='category' && $layout=='blog') ||			
			//full item
			($view=='article' && $layout=='default') ||			
			//archive, bots don't seem to be called in archive ? and not in lists either :-(#
			//($view=='archive')
			//frontpage
			($view=='frontpage') ||		
			//section blog
			($view=='section' && $layout=='blog')			
			))){					
				
				//get usergroup					
				$fua_usergroup = $this->get_usergroup_from_database();						
				
				//check item access
				if($this->fua_config['items_active']){
					
					$item_access_array = $this->get_item_access_from_database();	
					
					$item_right = $row_id.'__'.$fua_usergroup;	
					
					if($this->fua_config['items_reverse_access']){
						if(in_array($item_right, $item_access_array)){
							$this->fua_no_access_article($row);			
						}
					}else{
						if(!in_array($item_right, $item_access_array)){
							$this->fua_no_access_article($row);						
						}
					}
				}
				
				//take this out for free version
				//check category access
				if($this->fua_config['categories_active']){
						
					$category_access_array = $this->get_category_access_from_database();	
					
					
					$row_catid = 0;
					if (is_object( $row )) {
						 if (isset( $row->catid ))
						 {
							$row_catid = $row->catid;
						}
					}
					
					$category_right = $row_catid.'__'.$fua_usergroup;				
					
					if($this->fua_config['category_reverse_access']){
						if(in_array($category_right, $category_access_array)){
							$this->fua_no_access_article($row);	
						}
					}else{
						if(!in_array($category_right, $category_access_array)){
							$this->fua_no_access_article($row);							
						}
					}
				}
				
				//take this out for free version
				//check section access
				if($this->fua_config['sections_active']){
	
					
					$section_access_array = $this->get_section_access_from_database();	
					
					$row_sectionid = 0;
					if (is_object( $row )) {
						 if (isset( $row->sectionid ))
						 {
							$row_sectionid = $row->sectionid;
						}
					}
					
					$section_right = $row_sectionid.'__'.$fua_usergroup;	
					
					if($this->fua_config['sections_reverse_access']){
						if(in_array($section_right, $section_access_array)){
							$this->fua_no_access_article($row);		
						}
					}else{
						if(!in_array($section_right, $section_access_array)){
							$this->fua_no_access_article($row);			
						}
					}
				}
				
				
							
			}//end if no super administrator	
		}//end if anything needs checking	
	}
	
	function fua_no_access_article($row){
		
		//include class
		if(file_exists(dirname(__FILE__).'/../../administrator/components/com_frontenduseraccess/controller.php')){			
			require_once(dirname(__FILE__).'/../../administrator/components/com_frontenduseraccess/controller.php');
			$class_fua = new frontenduseraccessController;
			if($class_fua->fua_check_trial_version()){
				if($this->fua_config['items_display_type']=='inline_text'){
					$row->text = $this->fua_config['message_no_item_access'];
				}else{
					//hide completely				
					$row->title = '';
					$row->created='';
					$row->modified='';
					$row->author='';
					$row->text = '<br class="fua_article_hide" />';
				}	
			}
		}
		
	}
		
	function get_item_access_from_database(){
	
		static $item_access_array;
		
		if(!$item_access_array){	
			
			$this->database->setQuery("SELECT itemid_groupid FROM #__fua_items");
			$item_access_array = $this->database->loadResultArray();	
			
		}
		
		return $item_access_array;
	}
	
	//take this out for free version
	function get_category_access_from_database(){
	
		static $category_access_array;
		
		if(!$category_access_array){	
			
			$this->database->setQuery("SELECT category_groupid FROM #__fua_categories");
			$category_access_array = $this->database->loadResultArray();	
			
		}
		
		return $category_access_array;
	}
	
	//take this out for free version
	function get_section_access_from_database(){
	
		static $section_access_array;
		
		if(!$section_access_array){	
			
			$this->database->setQuery("SELECT section_groupid FROM #__fua_sections");
			$section_access_array = $this->database->loadResultArray();	
			
		}
		
		return $section_access_array;
	}
	
	function get_usergroup_from_database(){
	
		static $fua_usergroup;
		
		if(!$fua_usergroup){
		
			if(!$this->user_id){
				//user is not logged in
				$fua_usergroup = '10';
			}else{
				$user_id = $this->user_id;
				$this->database->setQuery("SELECT group_id FROM #__fua_userindex WHERE user_id='$user_id' LIMIT 1 ");		
				$rows_group = $this->database->loadObjectList();			
				$row_group = $rows_group[0];
				$fua_usergroup = $row_group->group_id;	
				
				if(!$fua_usergroup){
					//user is logged in, but is not assigned to any usergroup, so make it 9
					$fua_usergroup = '9';
				}
			}	
		}	
		return $fua_usergroup;
	}
	
	
	

}
?>